//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================


#include "evcarm.h"

EXPENTRY(__rt_sdiv)
    B   x_divide
EXIT(__rt_sdiv)

EXPENTRY(__rt_udiv)
    B   x_udivide
EXIT(__rt_udiv)

EXPENTRY(__rt_sdiv64by64)
    stmdb    r13!,{r4-r7,r14}
    mov      r4,r0
    mov      r5,r1
    mov      r6,r2
    mov      r7,r3
__ll_sdiv1:
    ands     r12,r7,#0x80000000
    bpl      pp1   @ (__ll_sdiv1 + 0x10)
    rsbs     r6,r6,#0
    rsc      r7,r7,#0
pp1:
    eors     r12,r12,r5,asr #32
    bcc      pp2  @ (__ll_sdiv1 + 0x20)
    rsbs     r4,r4,#0
    rsc      r5,r5,#0
pp2:
    mov      r0,#0
    mov      r1,#0
    mov      r3,r5
    mov      r2,r4
    teq      r7,#0
    teqeq    r6,#0
    beq      pp6    @(__ll_sdiv1 + 0x90)
    movs     r14,#0
pp3:
    adds     r6,r6,r6
    adcs     r7,r7,r7
    bcs      pp4    @(__ll_sdiv1 + 0x60)
    cmp      r7,r3
    cmpeq    r6,r2
    addls    r14,r14,#1
    bls      pp3    @(__ll_sdiv1 + 0x40)
    adds     r14,r14,#0
pp4:
    movs     r7,r7,rrx
    mov      r6,r6,rrx
pp5:
    subs     r4,r2,r6
    sbcs     r5,r3,r7
    movcs    r3,r5
    movcs    r2,r4
    adcs     r0,r0,r0
    adc      r1,r1,r1
    movs     r7,r7,lsr #1
    mov      r6,r6,rrx
    subs     r14,r14,#1
    bge      pp5    @(__ll_sdiv1 + 0x68)
pp6:
    movs     r12,r12,lsl #1
    bcc      pp7    @(__ll_sdiv1 + 0xa0)
    rsbs     r0,r0,#0
    rsc      r1,r1,#0
pp7:
    movs     r12,r12,lsl #1
    bcc      pp8    @(__ll_sdiv1 + 0xb0)
    rsbs     r2,r2,#0
    rsc      r3,r3,#0
pp8:
    ldmia    r13!,{r4-r7,pc}
EXIT(__rt_sdiv64by64)
@=============================================================
EXPENTRY(__rt_srem64by64)
    stmdb     sp!, {r4 - r7, r11, lr}  @ stmfd
    mov       r4, r0
    mov       r5, r1
    mov       r7, r3
    orrs      r3, r4, r5
    mov       r6, r2
    moveq     r0, #0
    moveq     r1, #0
    beq       pp346     @  pc+8+116
    orrs      r3, r6, r7
    bne       pp347     @  pc+8+44
    mov       r0, #3, 2  @ 0xC0000000 = 3221225472
    ldr       r11, [pc, #0x20]  @  pc+8+32 = 1003848C
    mov       r3, #0
    mov       r2, #0
    mov       r1, #0
    orr       r0, r0, #0x94  @ 0x94 = 148
    mov       lr, pc
    mov       pc, r11
    mvn       r0, #0
    mvn       r1, #0
    b         pp346     @  pc+8+64
    DCD 0xF000FE38  @ syscall may cause error
pp347:
    mov       r0, r4
    mov       r1, r5
    mov       r2, r6
    mov       r3, r7
    bl        C_SYMBOL_NAME(__rt_sdiv64by64)  @ 100382E4
    mov       r3, r1
    mov       r2, r0
    mov       r1, r6
    mul       lr, r2, r7
    mul       r2, r3, r1
    add       r3, lr, r2
    umull     lr, r2, r0, r1
    mul       r1, r0, r1
    add       r3, r3, r2
    subs      r0, r4, r1
    sbc       r1, r5, r3
pp346:
    ldmia     sp!, {r4 - r7, r11, lr}  @ ldmfd
    mov       pc, lr
EXIT(__rt_srem64by64)
//=============================================================
EXPENTRY(__rt_udiv64by64)
    stmdb     sp!, {r4 - r8, r11, lr}  @ stmfd
    mov       r7, #0
    mov       r8, #0
    mov       r4, #1
    mov       lr, #0
    orrs      r11, r2, r3
    bne       pp318     @  pc+8+44
    mov       r0, #3, 2  @ 0xC0000000 = 3221225472
    ldr       r11, [pc, #0x20]  @  pc+8+32 = 10038224
    mov       r3, #0
    mov       r2, #0
    mov       r1, #0
    orr       r0, r0, #0x94  @ 0x94 = 148
    mov       lr, pc
    mov       pc, r11
    mvn       r0, #0
    mvn       r1, #0
    b         pp319     @  pc+8+180
    DCD 0xF000FE38  @ syscall may cause error
pp318:
    cmp       r3, r1
    bhi       pp320     @  pc+8+128
    bcc       pp321     @  pc+8+4
    cmp       r2, r0
    bhi       pp322     @  pc+8+80
pp321:
    and       r5, r2, #0
    and       r6, r3, #2, 2  @ 0x80000000 = 2147483648
    orrs      r11, r5, r6
    bne       pp323     @  pc+8+44
    mov       r3, r3, lsl #1
    mov       lr, lr, lsl #1
    orr       r3, r3, r2, lsr #31
    orr       lr, lr, r4, lsr #31
    mov       r4, r4, lsl #1
    mov       r2, r2, lsl #1
    cmp       r3, r1
    bcc       pp321     @  pc+8-52
    bhi       pp325     @  pc+8+88
    cmp       r2, r0
    bls       pp321     @  pc+8-64
    b         pp325     @  pc+8+76
pp323:
    subs      r0, r0, r2
    sbc       r1, r1, r3
    mov       r7, r4
    mov       r8, lr
    b         pp325     @  pc+8+56
pp322:
    cmp       r3, r1
    bhi       pp320     @  pc+8+24
    bcc       pp330     @  pc+8+4
    cmp       r2, r0
    bhi       pp320     @  pc+8+12
pp330:
    adds      r7, r4, r7
    adc       r8, lr, r8
    subs      r0, r0, r2
    sbc       r1, r1, r3
pp320:
    mov       r2, r2, lsr #1
    mov       r11, r4, lsr #1
    orr       r2, r2, r3, lsl #31
    orr       r4, r11, lr, lsl #31
    mov       r3, r3, lsr #1
    mov       lr, lr, lsr #1
pp325:
    orrs      r11, r4, lr
    bne       pp322     @  pc+8-72
    mov       r0, r7
    mov       r1, r8
pp319:
    ldmia     sp!, {r4 - r8, r11, lr}  @ ldmfd
    mov       pc, lr
EXIT(__rt_udiv64by64)
//=============================================================
EXPENTRY(__rt_urem64by64)
    stmdb     sp!, {r4 - r7, r11, lr}  @ stmfd
    mov       r4, r0
    mov       r5, r1
    mov       r7, r3
    orrs      r3, r4, r5
    mov       r6, r2
    moveq     r0, #0
    moveq     r1, #0
    beq       pp343     @  pc+8+116
    orrs      r3, r6, r7
    bne       pp344     @  pc+8+44
    mov       r0, #3, 2  @ 0xC0000000 = 3221225472
    ldr       r11, [pc, #0x20]  @  pc+8+32 = 100383E8
    mov       r3, #0
    mov       r2, #0
    mov       r1, #0
    orr       r0, r0, #0x94  @ 0x94 = 148
    mov       lr, pc
    mov       pc, r11
    mvn       r0, #0
    mvn       r1, #0
    b         pp343     @  pc+8+64
    DCD 0xF000FE38  @ syscall may cause error
pp344:
    mov       r0, r4
    mov       r1, r5
    mov       r2, r6
   mov       r3, r7
    bl        C_SYMBOL_NAME(__rt_udiv64by64)  @ 100381DC
    mov       r3, r1
    mov       r2, r0
    mov       r1, r6
    mul       lr, r2, r7
    mul       r2, r3, r1
    add       r3, lr, r2
    umull     lr, r2, r0, r1
    mul       r1, r0, r1
    add       r3, r3, r2
    subs      r0, r4, r1
    sbc       r1, r5, r3
pp343:
    ldmia     sp!, {r4 - r7, r11, lr}  // ldmfd
    mov       pc, lr
EXIT(__rt_urem64by64)

EXPENTRY(__rt_srsh)
    cmp       r2, #0
    moveq     pc, lr
    cmp       r2, #0x20
    blt       shift31
    cmp       r2, #0x40
    blt       shift63
    mov       r1, r1, asr #32
    mov       r0, r1
    mov       pc, lr
shift63:
    and       r2, r2, #0x1F
    mov       r0, r1, asr r2
    mov       r1, r1, asr #32
    mov       pc, lr
shift31:
    mov       r0, r0, lsr r2
    rsb       r3, r2, #0x20
    orr       r0, r0, r1, lsl r3
    mov       r1, r1, asr r2
    mov       pc, lr
EXIT(__rt_srsh)

.end

